import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class test {
    // 存储学生成绩的列表
    private static List<StudentGrade> gradeList = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        showMainMenu();
    }

    // 主菜单
    private static void showMainMenu() {
        while (true) {
            System.out.println("\n欢迎使用学生成绩管理系统");
            System.out.println("请选择操作:");
            System.out.println("1.记录学生成绩");
            System.out.println("2.查询学生成绩");
            System.out.println("3.统计课程成绩");
            System.out.println("4.退出系统");
            System.out.print("请输入选项序号: ");

            String choice = scanner.nextLine();
            
            switch (choice) {
                case "1":
                    recordGrade();
                    break;
                case "2":
                    queryGrade();
                    break;
                case "3":
                    statisticsGrade();
                    break;
                case "4":
                    System.out.println("\n感谢使用学生成绩管理系统，再见!");
                    System.exit(0);
                default:
                    System.out.println("输入错误，请重新输入!");
            }
        }
    }

    // 记录学生成绩
    private static void recordGrade() {
        System.out.println("\n=====记录学生成绩=====");
        
        System.out.print("请输入学生姓名: ");
        String name = scanner.nextLine();
        
        System.out.print("请输入学生学号: ");
        String studentId = scanner.nextLine();
        
        // 检查学号是否已存在
        if (isStudentIdExists(studentId)) {
            System.out.println("错误: 该学号已存在!");
            return;
        }
        
        System.out.print("请输入课程名称: ");
        String course = scanner.nextLine();
        
        System.out.print("请输入成绩(0-100): ");
        String scoreStr = scanner.nextLine();
        
        try {
            double score = Double.parseDouble(scoreStr);
            if (score < 0 || score > 100) {
                System.out.println("错误: 成绩必须在0-100之间!");
                return;
            }
            
            StudentGrade grade = new StudentGrade(name, studentId, course, score);
            gradeList.add(grade);
            System.out.println("成绩已成功记录!");
        } catch (NumberFormatException e) {
            System.out.println("错误: 请输入有效的数字成绩!");
        }
    }

    // 查询学生成绩
    private static void queryGrade() {
        System.out.println("\n=====查询学生成绩=====");
        System.out.println("请选择查询方式:");
        System.out.println("1.按学生姓名查询");
        System.out.println("2.按学生学号查询");
        System.out.println("3.按课程名称查询");
        System.out.print("请输入选项序号: ");
        
        String choice = scanner.nextLine();
        
        switch (choice) {
            case "1":
                queryByName();
                break;
            case "2":
                queryByStudentId();
                break;
            case "3":
                queryByCourse();
                break;
            default:
                System.out.println("输入错误，请重新输入!");
        }
    }

    // 按姓名查询
    private static void queryByName() {
        System.out.print("请输入学生姓名: ");
        String name = scanner.nextLine();
        
        List<StudentGrade> result = new ArrayList<>();
        for (StudentGrade grade : gradeList) {
            if (grade.getName().equalsIgnoreCase(name)) {
                result.add(grade);
            }
        }
        
        displayQueryResult(result);
    }

    // 按学号查询
    private static void queryByStudentId() {
        System.out.print("请输入学生学号: ");
        String studentId = scanner.nextLine();
        
        List<StudentGrade> result = new ArrayList<>();
        for (StudentGrade grade : gradeList) {
            if (grade.getStudentId().equals(studentId)) {
                result.add(grade);
            }
        }
        
        displayQueryResult(result);
    }

    // 按课程查询
    private static void queryByCourse() {
        System.out.print("请输入课程名称: ");
        String course = scanner.nextLine();
        
        List<StudentGrade> result = new ArrayList<>();
        for (StudentGrade grade : gradeList) {
            if (grade.getCourse().equalsIgnoreCase(course)) {
                result.add(grade);
            }
        }
        
        displayQueryResult(result);
    }

    // 显示查询结果
    private static void displayQueryResult(List<StudentGrade> result) {
        if (result.isEmpty()) {
            System.out.println("未找到匹配的成绩记录!");
        } else {
            System.out.println("查询结果:");
            for (StudentGrade grade : result) {
                System.out.printf("姓名: %s，学号: %s，课程: %s，成绩: %.1f%n",
                        grade.getName(), grade.getStudentId(), grade.getCourse(), grade.getScore());
            }
        }
    }

    // 统计课程成绩
    private static void statisticsGrade() {
        System.out.println("\n=====统计课程成绩=====");
        System.out.print("请输入课程名称: ");
        String course = scanner.nextLine();
        
        double sum = 0;
        int count = 0;
        double max = Double.MIN_VALUE;
        double min = Double.MAX_VALUE;
        
        for (StudentGrade grade : gradeList) {
            if (grade.getCourse().equalsIgnoreCase(course)) {
                double score = grade.getScore();
                sum += score;
                count++;
                
                if (score > max) {
                    max = score;
                }
                
                if (score < min) {
                    min = score;
                }
            }
        }
        
        if (count == 0) {
            System.out.println("未找到该课程的成绩记录!");
        } else {
            double average = sum / count;
            System.out.printf("课程: %s%n", course);
            System.out.printf("平均分: %.2f%n", average);
            System.out.printf("最高分: %.1f%n", max);
            System.out.printf("最低分: %.1f%n", min);
        }
    }

    // 检查学号是否已存在
    private static boolean isStudentIdExists(String studentId) {
        for (StudentGrade grade : gradeList) {
            if (grade.getStudentId().equals(studentId)) {
                return true;
            }
        }
        return false;
    }
}

//、
// 学生成绩类
class StudentGrade {
    private String name;
    private String studentId;
    private String course;
    private double score;

    public StudentGrade(String name, String studentId, String course, double score) {
        this.name = name;
        this.studentId = studentId;
        this.course = course;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public String getStudentId() {
        return studentId;
    }

    public String getCourse() {
        return course;
    }

    public double getScore() {
        return score;
    }
}
//.
